package com.heima.comment.controller;

import com.heima.comment.dtos.CommentDto;
import com.heima.comment.dtos.CommentLikeDto;
import com.heima.comment.dtos.CommentSaveDto;
import com.heima.comment.service.ApCommentService;
import com.heima.comment.vos.CommentVo;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/comment")
@Slf4j
public class ApCommentController {

    @Autowired
    private ApCommentService apCommentService;

    /**
     * 保存文章评论
     *
     * @param commentSaveDto 前端dto
     * @return 响应结果
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody CommentSaveDto commentSaveDto) {
        log.info("评论保存{}:", commentSaveDto);
        apCommentService.save(commentSaveDto);
        return ResponseResult.okResult(null);
    }

    /**
     * 文章评论加载
     * @param commentDto 前端dto
     * @return 响应结果
     */
    @PostMapping("/load")
    public ResponseResult<List<CommentVo>> load(@RequestBody CommentDto commentDto){
        List<CommentVo> load = apCommentService.load(commentDto);
        return ResponseResult.okResult(load);
    }

    /**
     * 文章评论点赞
     * @param commentLikeDto 前端dto
     * @return 响应结果
     */
    @PostMapping("/like")
    public ResponseResult<Map<String, Integer>> like(@RequestBody CommentLikeDto commentLikeDto) {
        Map<String, Integer> like = apCommentService.like(commentLikeDto);
        return ResponseResult.okResult(like);
    }

}
